{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "433a223e-beef-469b-beca-aec37a2c0e84",
   "metadata": {
    "tags": []
   },
   "source": [
    "执行命令需要在同一行命令中，先source环境名(base,python27,python36,python37,python38,python39,python310,cube-studio)才能pip安装到指定环境，如果不知道有哪些虚拟环境，可以conda info --envs查看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d3a6783f-931c-495b-9f44-a8f4ad98da37",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pandas in /root/miniconda3/envs/cube-studio/lib/python3.9/site-packages (2.0.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /root/miniconda3/envs/cube-studio/lib/python3.9/site-packages (from pandas) (2.8.2)\n",
      "Requirement already satisfied: numpy>=1.20.3 in /root/miniconda3/envs/cube-studio/lib/python3.9/site-packages (from pandas) (1.24.3)\n",
      "Requirement already satisfied: tzdata>=2022.1 in /root/miniconda3/envs/cube-studio/lib/python3.9/site-packages (from pandas) (2023.3)\n",
      "Requirement already satisfied: pytz>=2020.1 in /root/miniconda3/envs/cube-studio/lib/python3.9/site-packages (from pandas) (2023.3)\n",
      "Requirement already satisfied: six>=1.5 in /root/miniconda3/envs/cube-studio/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n",
      "\u001B[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001B[0m\u001B[33m\n",
      "\u001B[0m"
     ]
    }
   ],
   "source": [
    "!source activate cube-studio && pip install pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "917dfe6dc3d5d6b1"
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e18c42e3-660a-40e4-abe7-5ae89e41a194",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from cubestudio.request.model_client import Client,init\n",
    "from cubestudio.train.task import Job_Template,Project,Pipeline,Task\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9f010be3-e690-41d9-94fe-706a607b3302",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初始化验证成功\n"
     ]
    }
   ],
   "source": [
    "# 初始化客户端\n",
    "HOST = \"http://kubeflow-dashboard.infra/\"\n",
    "token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhZG1pbiJ9.j6-hUMaFYdSIzfc6i6TJ5DaS96Z9I78SrjxAOg-71yE'\n",
    "username='admin'\n",
    "init(host=HOST,username=username,token=token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "447b77c2-41dc-41d8-b3e5-bb0695b31af1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 添加一个画布\n",
    "pipeline = Client(Pipeline).add_or_update(\n",
    "    name=f'{username}-default',\n",
    "    describe='sdk画布',\n",
    "    project=Client(Project).one(name='public')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "326c6a64-d1d7-4150-a081-fac8a081f5ad",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pipeline 为修改时多余字段\n",
      "job_template 为修改时多余字段\n"
     ]
    }
   ],
   "source": [
    "# 添加一个任务\n",
    "job_template = Client(Job_Template).one(name=\"自定义镜像\")\n",
    "task=Client(Task).add_or_update(\n",
    "    name='sdk-test1',\n",
    "    label='sdk发起的任务',\n",
    "    pipeline=pipeline,\n",
    "    job_template=Client(Job_Template).one(name=\"自定义镜像\"),\n",
    "    timeout=3600,\n",
    "    retry=0,\n",
    "    args=json.dumps(\n",
    "        {\n",
    "            \"images\":\"ubuntu:20.04\",\n",
    "            \"command\":'for i in {1..50}; do date; sleep 1; done',\n",
    "            \"workdir\":\"/\"\n",
    "        }\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e7dfe7f1-8857-4464-b9a2-1f81dd7c514d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "任务启动成功，日志地址：http://10.101.140.141/k8s/web/log/dev/pipeline/run-admin-default-sdk-test1\n",
      "\"时间：\"2023-06-09 16:02:25 ，类型：Normal ，原因：Pulling ，消息：Pulling image \"ubuntu:18.04\"\n",
      "\"时间：\"2023-06-09 16:02:29 ，类型：Normal ，原因：Pulled ，消息：Successfully pulled image \"ubuntu:18.04\" in 4.733362579s\n",
      "\"时间：\"2023-06-09 16:02:29 ，类型：Normal ，原因：Created ，消息：Created container run-admin-default-sdk-test1\n",
      "\"时间：\"2023-06-09 16:02:30 ，类型：Normal ，原因：Started ，消息：Started container run-admin-default-sdk-test1\n",
      "Running\n"
     ]
    }
   ],
   "source": [
    "task.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9b094324-a470-4172-a66c-b2ef493f3501",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fri Jun  9 16:02:31 CST 2023\n",
      "Fri Jun  9 16:02:32 CST 2023\n",
      "Fri Jun  9 16:02:33 CST 2023\n",
      "Fri Jun  9 16:02:34 CST 2023\n",
      "Fri Jun  9 16:02:35 CST 2023\n",
      "\n",
      "Fri Jun  9 16:02:36 CST 2023\n",
      "Fri Jun  9 16:02:37 CST 2023\n",
      "Fri Jun  9 16:02:38 CST 2023\n",
      "Fri Jun  9 16:02:39 CST 2023\n",
      "Fri Jun  9 16:02:40 CST 2023\n",
      "\n",
      "Fri Jun  9 16:02:41 CST 2023\n",
      "Fri Jun  9 16:02:42 CST 2023\n",
      "Fri Jun  9 16:02:43 CST 2023\n",
      "Fri Jun  9 16:02:44 CST 2023\n",
      "Fri Jun  9 16:02:45 CST 2023\n",
      "\n",
      "Fri Jun  9 16:02:46 CST 2023\n",
      "Fri Jun  9 16:02:47 CST 2023\n",
      "Fri Jun  9 16:02:48 CST 2023\n",
      "Fri Jun  9 16:02:49 CST 2023\n",
      "Fri Jun  9 16:02:50 CST 2023\n",
      "\n",
      "Fri Jun  9 16:02:51 CST 2023\n",
      "Fri Jun  9 16:02:52 CST 2023\n",
      "Fri Jun  9 16:02:53 CST 2023\n",
      "Fri Jun  9 16:02:54 CST 2023\n",
      "Fri Jun  9 16:02:55 CST 2023\n",
      "\n",
      "Fri Jun  9 16:02:56 CST 2023\n",
      "Fri Jun  9 16:02:57 CST 2023\n",
      "Fri Jun  9 16:02:58 CST 2023\n",
      "Fri Jun  9 16:02:59 CST 2023\n",
      "Fri Jun  9 16:03:00 CST 2023\n",
      "\n",
      "Fri Jun  9 16:03:01 CST 2023\n",
      "Fri Jun  9 16:03:02 CST 2023\n",
      "Fri Jun  9 16:03:03 CST 2023\n",
      "Fri Jun  9 16:03:04 CST 2023\n",
      "Fri Jun  9 16:03:05 CST 2023\n",
      "\n",
      "Fri Jun  9 16:03:06 CST 2023\n",
      "Fri Jun  9 16:03:07 CST 2023\n",
      "Fri Jun  9 16:03:08 CST 2023\n",
      "Fri Jun  9 16:03:09 CST 2023\n",
      "Fri Jun  9 16:03:10 CST 2023\n",
      "\n",
      "Fri Jun  9 16:03:12 CST 2023\n",
      "Fri Jun  9 16:03:13 CST 2023\n",
      "Fri Jun  9 16:03:14 CST 2023\n",
      "Fri Jun  9 16:03:15 CST 2023\n",
      "Fri Jun  9 16:03:16 CST 2023\n",
      "\n",
      "Fri Jun  9 16:03:17 CST 2023\n",
      "Fri Jun  9 16:03:18 CST 2023\n",
      "Fri Jun  9 16:03:19 CST 2023\n"
     ]
    }
   ],
   "source": [
    "task.log(follow=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2f67a77d-b41b-424a-abae-10849c94985b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "task.stop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06cbe10e-1984-48d4-a048-1a852e7fdf4e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cube-studio",
   "language": "python",
   "name": "cube-studio"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
